﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sim
{
    public partial class FuzzyQuery : Form
    {
        public FuzzyQuery()
        {
            InitializeComponent();
        }

        public new class Region
        {
            public string id { get; set; }
            public string name { get; set; }
        }

        //组合框
        private void _Region()
        {
            List<Region> region = new List<Region>();
            region.Add(new Region { id = "Id", name = "书号" });
            region.Add(new Region { id = "Title", name = "文章标题" });
            region.Add(new Region { id = "Author", name = "文章作者" });
            comboBox1.DataSource = region;
            comboBox1.ValueMember = "id";
            comboBox1.DisplayMember = "name";
        }

        private void FuzzyQuery_Load(object sender, EventArgs e)
        {
            _Region();
        }

        //模糊查询方法
        private void FuzzyQueryMethod(string sql)
        {
            var dt = DbHelper.GetDbHelper(sql);

            dataGridView1.DataSource = dt;

            //判断有没有相应数据
            if (dataGridView1.Rows.Count <= 0)
            {
                MessageBox.Show("没有查询到相应数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            //设置第 1 列的列标题（Id）
            dataGridView1.Columns[0].HeaderText = "ID";
            //设置第 2 列的列标题（Title）
            dataGridView1.Columns[1].HeaderText = "文章标题";
            //设置第 3 列的列标题（Author）
            dataGridView1.Columns[2].HeaderText = "文章作者";
            //设置第 4 列的列标题（CreatedTime）
            dataGridView1.Columns[3].HeaderText = "创建时间";

            //设置数据表格为只读
            dataGridView1.ReadOnly = true;
            //不允许添加行
            dataGridView1.AllowUserToAddRows = false;
            //背景为白色
            dataGridView1.BackgroundColor = Color.White;
            //只允许选中单行
            dataGridView1.MultiSelect = false;
            //整行选中
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        //查询按钮
        private void button1_Click(object sender, EventArgs e)
        {
            string combobox1_index = this.comboBox1.SelectedValue.ToString();

            string sql = "select * from Articles Where {0} like '%{1}%'";

            sql = string.Format(sql, combobox1_index, textBox1.Text);

            FuzzyQueryMethod(sql);
        }

        //在查询框按下Enter键登入
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button1_Click(sender, e);
            }
        }
    }
}
